from google.appengine.ext import db
from google.appengine.ext.db import GqlQuery
from models import *

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app 
import analyze_website

from datetime import datetime
import logging
import string
import math
from google.appengine.api.urlfetch_errors import DeadlineExceededError

import util
import Consts

#####################################
#tasks
#####################################

###
#search for old business and deal from auto parseing and delete them
#search foe low rate deals and delete them
class DataBaseCleanHandler(webapp.RequestHandler):
    
    def get(self):
        
        #delete fake deals
        deal_lst = Deal.all()
        deal_lst.filter("fake_num >=", Consts.FAKE_DEAL_LIMIT)
        for deal in deal_lst:
            deal._deleted = 1
        #delete old business added by auto_add
        business_list = Business.all()
        for business in business_list:
            if (business._source_type != "user"):
                
                today = datetime.today()
                today_sum = today.year*365 + today.month*30 + today.day
                
                added_sum = business._dateAdded.year*365 + business._dateAdded.month*30 + business._dateAdded.day
           
                if (today_sum - added_sum > Consts.OLD_IN_DAYS):
                    
                    deal_list = Deal.all()
                    #get relevant deals per business
                    deal_list.filter("business_item = ", business)
                    
                    #delete all the relevant
                    db.delete(deal_list)
                    db.delete(business)
                    
            #delete low rate deals
            deal_list = Deal.all()
            deal_list.filter("_source_type ==", "user")
            deal_list.filter("rate_num > ", Consts.MIN_USERS_RATED)
            for deal in deal_list:
                rate = deal.rate_sum/deal.rate_num
                if (rate < Consts.LOW_RATE):
                    deal._delete = 1
        
        
###
#start new auto add
class DataBaseNewAutoAddHandler(webapp.RequestHandler):
    def get(self):
        #2eat bars
        next_url = NextUrl()
        next_url.http_name = "2eat_bars"
        next_url.next_url = analyze_website.URL_2EAT_BARS
        next_url.put()
        
        #rests in Israel
        next_url = NextUrl()
        next_url.http_name = "rests in Israel"
        next_url.next_url = analyze_website.URL_RESTS_IN_ISRAEL
        next_url.put()
        
###
#run different auto parse in order to find deals and business
class DataBaseAutoAddHandler(webapp.RequestHandler):
    
    def get(self):
        
        logging.info("Starting auto parse")
        try:
            next_url = NextUrl.all().get()
            if (next_url == None):
                logging.info("nothing to do")
                return
        
            if (next_url.http_name == "2eat_bars"):
                #parse 2eat websites for restaurants  and deals
                analyze_website.analyze_2eat_bars(next_url)
            elif (next_url.http_name == "rests in Israel"):
                analyze_website.analyze_rest_in_israel(next_url)
            else:
                logging.info("nothing to do")
        except DeadlineExceededError:
            logging.info("Finish auto parse: not in time")
            return
        
        logging.info("Finish auto parse: in time")

##
#calc credits of each user by the rating of his deals and comments
class CalcUserCreditsHandler(webapp.RequestHandler):
    
    def get(self, *args):
        
        logging.info("Start calc users credit task")
        
        user_list = User.all()
        
        logging.info("found {0} users".format(user_list.count()))
                     
        for user in user_list:
            rate_num = 0
            rate_sum = 0
            
            #count deal rate
            deal_list = Deal.all()
            deal_list.filter("_source ==", user.email)
            deal_list.filter("rate_num !=", 0)

            for deal in deal_list:
                rate_num = rate_num + deal.rate_num
                rate_sum = deal.rate_sum
            
            #count comment rate
            comment_list = Comment.all()
            comment_list.filter("_source ==", user.email)
            comment_list.filter("comment_rate_num !=", 0)

            for comment in comment_list:
                rate_num = rate_num + comment.comment_rate_num
                rate_sum = comment.comment_rate_sum
            
            #calc user rate
            if (rate_num == 0):
                user_rate = 0
            else:
                user_rate = rate_sum/rate_num
            
            #calc user credit
            user.credit = Consts.DEFAULT_CREDIT + user_rate


class GeoPointHandler(webapp.RequestHandler):
    def get(self, *args):
        logging.info("Starting geo point calculation")       
        business_lst = Business.all()
        business_lst.filter("_deleted == ", 0)
        
        for business in business_lst:
            util.lat_lon_lookup(business) 
        
        logging.info("Finish geo point calculation")
        

class CheckInCleanHandlerHandler(webapp.RequestHandler):
    def get(self, *args):
        logging.info("Starting checkins cleaner")
        checkin_lst = CheckIn.all()
        db.delete(checkin_lst)
        logging.info("finish checkins cleaner")

application = webapp.WSGIApplication([('/tasks/new_auto_add', DataBaseNewAutoAddHandler), \
                                      ('/tasks/auto_add', DataBaseAutoAddHandler), \
                                      ('/tasks/clean', DataBaseCleanHandler), \
                                      ('/tasks/calc_users_credit', CalcUserCreditsHandler), \
                                      ('/tasks/geopoint', GeoPointHandler),
                                      ('/tasks/checkin_clean', CheckInCleanHandlerHandler)], debug=True)


def main():
    run_wsgi_app(application)

if __name__ == '__main__':
    main()

    

